package com.itheima.reggie.contreller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import com.itheima.reggie.service.impl.EmployeeServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.security.DigestException;
import java.time.LocalDateTime;
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;


    /*①. 将页面提交的密码password进行md5加密处理, 得到加密后的字符串
    ②. 根据页面提交的用户名username查询数据库中员工数据信息
    ③. 如果没有查询到, 则返回登录失败结果
    ④. 密码比对，如果不一致, 则返回登录失败结果
    ⑤. 查看员工状态，如果为已禁用状态，则返回员工已禁用结果
    ⑥. 登录成功，将员工id存入Session, 并返回登录成功结果
    */
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request){
        String password = employee.getPassword();
        String md5password = DigestUtils.md5DigestAsHex(password.getBytes());
        //2
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<Employee>();
        lambdaQueryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp=employeeService.getOne(lambdaQueryWrapper);
        //3
        if (emp==null){
            return R.error("用户名或密码错误.请重试");
        }
        //4
        if (!md5password.equals(emp.getPassword())){
            return R.error("用户名或密码错误");
        }
        //5
        if (emp.getStatus()==0){
            return R.error("用户名已被禁用");
        }
        //6.
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }

    //登出功能
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest req){
        req.getSession().removeAttribute("employee");
        return R.success("");
    }
    @PostMapping
    public R<String> save(@RequestBody Employee employee,HttpServletRequest request){
        log.info("新增员工,员工信息:{}",employee.toString());
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employee.setCreateTime(LocalDateTime.now());
        employee.setCreateUser((long)request.getSession().getAttribute("employee"));
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser((long)request.getSession().getAttribute("employee"));

        employeeService.save(employee);


        return R.success("添加成功");
    }
    //员工信息分页查询
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        //构造条件构造器
        Page pageinfo= new Page(page,pageSize);
            LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
            //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //添加排序条件
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        //执行查询
        employeeService.page(pageinfo,queryWrapper);
        log.info("查询数目:{}",page);
        return R.success(pageinfo);
    }

    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable long id){
        Employee employee = employeeService.getById(id);
        log.info("修改:{}",employee);
        if (employee!=null){
         return  R.success(employee);
        }
        return R.error("没有查询对应员工信息");
    }

    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){
        log.info(employee.toString());

        Long empId = (Long)request.getSession().getAttribute("employee");
        log.info("user:{}",empId);
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(empId);
        employeeService.updateById(employee);

        return R.success("员工信息修改成功");
    }
}
